<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class UserController extends Controller
{
  public function __construct(Request $request)
  {
    parent::__construct($request, 'User', 'users');
  }

  public function getRelatedEntityList(String $bearer, String $table)
  {
    $coreApiEndpointUrl = '';
    $resourcesData = $resources = null;
    $coreApiEndpointUrl = $this->coreApiEndpoint . "/" . $table;

    $resourcesData = getFromCoreApi($bearer, $coreApiEndpointUrl);
    $resources = $resourcesData['data'];

    return $resources;
  }

  public function edit(Request $request, $id)
  {
    Log::info(__METHOD__);

    $coreApiEndpointUrl = '';
    $resourcesData = $resources = $response = null;
    $session = $request->session();
    $bearer = $session->get('coreapi_token');
    $backurl = url()->previous();
    $backurl = removeSearchFromUrl($backurl);
    $currenturl = url()->current();
    $roles = $this->getRelatedEntityList($bearer, 'roles');
    $people = $this->getRelatedEntityList($bearer, 'people');
    $companies = $this->getRelatedEntityList($bearer, 'companies');

    $params = $request->query();
    $coreApiEndpointUrl = $this->coreApiEndpoint . "/{$this->endpoint}/$id";
    if (isset($params['include'])) {
      $coreApiEndpointUrl = addIncludeToUrl($coreApiEndpointUrl, $params['include']);
    }
    $resourcesData = getFromCoreApi($bearer, $coreApiEndpointUrl);
    $resource = $resourcesData['resource'];
    $response = view($this->endpoint . '.edit', compact('resource', 'roles', 'people', 'companies', 'backurl', 'currenturl'));

    return $response;
  }

  public function update(Request $request, $id)
  {
    Log::info(__METHOD__);

    $coreApiEndpointUrl = '';
    $resourcesData = $resource = $response = null;
    $session = $request->session();
    $bearer = $session->get('coreapi_token');
    $backurl = '/' . $this->endpoint;
    $backurl = removeSearchFromUrl($backurl);
    $currenturl = url()->current() . '?include[]=picture&include[]=role&include[]=person&include[]=company';
    $params['include'] = ['picture', 'role', 'person', 'company'];

    $data = $request->all();
    $coreApiEndpointUrl = $this->coreApiEndpoint . "/{$this->endpoint}/$id";
    $resourcesData = putToCoreApi($bearer, $coreApiEndpointUrl, $data);
    if (isset($resourcesData['resource'])) {
      $coreApiEndpointUrl = addIncludeToUrl($coreApiEndpointUrl, $params['include']);
      $resourcesData = getFromCoreApi($bearer, $coreApiEndpointUrl);
      $resource = $resourcesData['resource'];
      $response = view($this->endpoint . '.show', compact('resource', 'backurl', 'currenturl'));
    }
    if (isset($resourcesData['error'])) {
      $resource = $data;
      $response = view($this->endpoint . '.show', compact('resource', 'backurl', 'currenturl'))->withErrors($resourcesData['error']);
    }

    return $response;
  }

  public function search(Request $request)
  {
    $coreApiEndpointUrl = $coreBgEndpointUrl = $fullEndpoint = $filterValue = '';
    $params = $endpointResponse = $resultsData = $results = $pagination = $links = $response = null;
    $session = $request->session();
    $bearer = $session->get('coreapi_token');
    $filterValue = $request->input('filter_email');
    $coreApiEndpointUrl = $this->coreApiEndpoint . "/{$this->endpoint}?";
    if (!empty($filterValue)) {
      $coreApiEndpointUrl = $coreApiEndpointUrl . "email=[lk]{$filterValue}&limit=-1";
    }
    else {
      $coreApiEndpointUrl = $coreApiEndpointUrl . "limit=" . env('DEFAULT_INDEX_LIMIT');
    }
    $request->request->add(['limit' => env('DEFAULT_INDEX_LIMIT')]);
    $params = $request->query();
    $coreApiEndpointUrl = addParamsToUrl($coreApiEndpointUrl, $params);
    $coreBgEndpointUrl = addParamsToUrl(env('CORE_BG_ENDPOINT') . '/' . $this->endpoint, $params);
    $fullEndpoint = env('CORE_BG_ENDPOINT') . '/' . $this->endpoint;
    $resultsData = getFromCoreApi($bearer, $coreApiEndpointUrl);
    $results = $resultsData['data'];
    $links = $resultsData['links'];
    $pagination = $resultsData['meta'];
    $links = str_replace(
      env('CORE_API_ENDPOINT'),
      env('CORE_BG_ENDPOINT'),
      $links
    );
    $response = view($this->endpoint . '.index', compact('results', 'pagination', 'links', 'fullEndpoint', 'filterValue'));

    return $response;
  }
}
